#include "main.h"
#include "key.h"
#include "oled.h"
#include "motor.h"
#include "dht11.h"
#include "pir.h"
#include "usart.h"
#include "delay.h"
#include <stdio.h>

SystemMode currentMode = MODE_SELECTION;
AutoSubMenu currentSubMenu = TEMP_CTRL;
uint8_t pirEnabled = 0;
int16_t temperature = 0;
int16_t humidity = 0;
uint8_t motorDirection = 0; // 0:停止, 1:正转, 2:反转
uint16_t motorSpeed = 0;
uint8_t menu_selection = 0; // 0:蓝牙模式, 1:自动模式

int main(void) {
    SystemClock_Config();
    GPIO_Configuration();
    
    delay_init();
    OLED_Init();
    KEY_Init();
    Motor_Init();
    DHT11_Init();
    PIR_Init();
    USART1_Init(9600);
    
    OLED_Clear();
    OLED_ShowString(0, 0, "Mode Select:", 16);
    OLED_ShowString(0, 2, ">Bluetooth", 16);
    OLED_ShowString(0, 4, " Auto Mode", 16);
    
    while (1) {
        KEY_Scan();
        
        switch(currentMode) {
            case MODE_SELECTION:
                // 模式选择界面处理在按键中断中
                break;
                
            case AUTO_MODE_MENU:
                // 自动模式菜单处理在按键中断中
                break;
                
            case BLUETOOTH_MODE:
                Bluetooth_Mode_Handler();
                break;
                
            case AUTO_MODE_RUNNING:
                Auto_Mode_Handler();
                delay_ms(1000); // 1秒更新一次
                break;
        }
    }
}

// 蓝牙模式处理
void Bluetooth_Mode_Handler(void) {
    OLED_ShowString(0, 0, "Bluetooth Mode", 16);
    OLED_ShowString(0, 2, "Speed:", 16);
    OLED_ShowNum(60, 2, motorSpeed, 3, 16);
    OLED_ShowString(0, 4, "Dir:", 16);
    OLED_ShowString(40, 4, motorDirection==1?"FWD":motorDirection==2?"REV":"STOP", 16);
}

// 自动模式处理
void Auto_Mode_Handler(void) {
    if(DHT11_Read_Data(&temperature, &humidity)) {
        uint8_t humanDetected = PIR_Read();
        uint16_t calcSpeed = 0;
        
        if(pirEnabled && humanDetected) {
            switch(currentSubMenu) {
                case TEMP_CTRL:
                    calcSpeed = temperature * 2;
                    break;
                case HUMIDITY_CTRL:
                    calcSpeed = humidity ;
                    break;
                case PIR_CTRL:
                    calcSpeed = 50; // 默认速度
                    break;
            }
            motorDirection = 1; // 正转
            motorSpeed = (calcSpeed > 100) ? 100 : calcSpeed;
        } else {
            motorSpeed = 0;
            motorDirection = 0;
        }
        
        Motor_SetSpeed(motorSpeed);
        Motor_SetDirection(motorDirection);
        
        OLED_Clear();
        OLED_ShowString(0, 0, "Temp:", 16);
        OLED_ShowNum(50, 0, temperature, 2, 16);
        OLED_ShowString(70, 0, "C", 16);
        
        OLED_ShowString(0, 2, "Humidity:", 16);
        OLED_ShowNum(80, 2, humidity, 2, 16);
        OLED_ShowString(100, 2, "%", 16);
        
        OLED_ShowString(0, 4, "PIR:", 16);
        OLED_ShowString(40, 4, pirEnabled?"ON ":"OFF", 16);
        OLED_ShowString(80, 4, humanDetected?"DET":"   ", 16);
        
        OLED_ShowString(0, 6, "Speed:", 16);
        OLED_ShowNum(50, 6, motorSpeed, 3, 16);
        OLED_ShowString(80, 6, "Dir:", 16);
        OLED_ShowString(110, 6, motorDirection==1?"FWD":motorDirection==2?"REV":"STOP", 16);
    }
}

void SystemClock_Config(void) {
    SystemInit();
    RCC_DeInit();
    RCC_HSEConfig(RCC_HSE_ON);
    while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
    
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
    RCC_PLLCmd(ENABLE);
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
    
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
    while(RCC_GetSYSCLKSource() != 0x08);
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | 
                          RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}

void GPIO_Configuration(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
    
    // 配置按键引脚
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
}